#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SLDestroy(SL* ps)
{
	if (ps)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SLCapacityCheck(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int NewCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* Np = (SLDataType*)realloc(ps->arr, NewCapacity * sizeof(SLDataType));
		if (Np == NULL)
		{
			perror("Realloc Fail");
			exit(1);
		}
		ps->arr = Np;
		ps->capacity = NewCapacity;
	}
}


//void SLPrint(SL s)
//{
//	int i = 0;
//	for (i = 0; i < s.size; i++)
//		printf("%d ", s.arr[i]);
//	printf("\n");
//}

void SLPushFront(SL* ps, SLDataType x)
{
	SLCapacityCheck(ps);
	assert(ps);
	int i = 0;
	for (i = ps->size; i > 0; i--)
		ps->arr[i] = ps->arr[i - 1];
	ps->arr[0] = x;
	ps->size++;
}

void SLPushBack(SL* ps, SLDataType x)
{
	SLCapacityCheck(ps);
	assert(ps);
	ps->arr[ps->size++] = x;
}

void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	int i = 0;
	ps->size--;
	for (i = 0; i < ps->size; i++)
		ps->arr[i] = ps->arr[i + 1];
}

void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	SLCapacityCheck(ps);
	assert(pos >= 0 && pos <= ps->size);
	int i = 0;
	for (i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size - 1);
	int i = 0;
	for (i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//int SLFind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	int i = 0;
//	for (i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//			return i;
//	}
//	return -1;
//}